/* ***** BEGIN LICENSE BLOCK *****
 *   Version: MPL 1.1/GPL 2.0/LGPL 2.1
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 * 
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is sudoku.
 *
 * The Initial Developer of the Original Code is
 * Petr Blahoš.
 * Portions created by the Initial Developer are Copyright (C) 2008
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s):
 *
 * Alternatively, the contents of this file may be used under the terms of
 * either the GNU General Public License Version 2 or later (the "GPL"), or
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 * in which case the provisions of the GPL or the LGPL are applicable instead
 * of those above. If you wish to allow use of your version of this file only
 * under the terms of either the GPL or the LGPL, and not to allow others to
 * use your version of this file under the terms of the MPL, indicate your
 * decision by deleting the provisions above and replace them with the notice
 * and other provisions required by the GPL or the LGPL. If you do not delete
 * the provisions above, a recipient may use your version of this file under
 * the terms of any one of the MPL, the GPL or the LGPL.
 * 
 * ***** END LICENSE BLOCK ***** */
EXPORTED_SYMBOLS = [
	"griddb",
	"GRID",
	"LOG"
];

var consoleService = Components.classes["@mozilla.org/consoleservice;1"].getService(Components.interfaces.nsIConsoleService);
function LOG(msg) {
	consoleService.logStringMessage(msg);
}

function GRID(id, gid, grid, rating, dt, best, sum, count) {
	this.id = id;
	this.grid = grid;
	this.rating = rating;
	this.dt = dt;
	this.best = best;
	this.sum = sum;
	this.count = count;
}

GRID.prototype = {
set_date: function() {
	var d = new Date();
	this.dt = d.getFullYear()<<16 | (d.getMonth()+1)<<8 | d.getDate();
},
get_date: function() {
	if (0==this.dt) {
		return "";
	}
	return "" + (this.dt >> 16) + "/" + ((this.dt>>8) & 0xFF) + "/" + (this.dt & 0xFF);
},
get_rating: function() {
	if (this.rating<0) {
		return "???";
	}
	return "" + this.rating;
},
get_best_time: function() {
	if (this.best<=0) {
		return "";
	}
	var ret = "" + Math.floor(this.best/60) + ":";
	var sec = this.best%60;
	if (sec>9) {
		ret+= sec;
	} else {
		ret+= "0" + sec;
	}
	return ret;
},
get_avg_time: function() {
	if (this.count<=0) {
		return "";
	}
	var tm0 = Math.round(this.sum/this.count);
	var ret = "" + Math.floor(tm0/60) + ":";
	var sec = tm0%60;
	if (sec>9) {
		ret+= sec;
	} else {
		ret+= "0" + sec;
	}
	return ret;
},
is_easy: function() {
	return this.rating>0 && this.rating<=50;
},
is_medium: function() {
	return this.rating>50 && this.rating<=60;
},
is_hard: function() {
	return this.rating>60;
},
__sentinel: 0
};

var griddb = {
conn: null,
init_data: [
//		[ "984750263256398147713246895529874316348615729167932458675423981892167534431589672", 1],
//		[ "100000002090400050006000700050903000000070000000850040700000600030009080002000001", 95 ],
//		[ "001004000000060305000900000800000703000000028500070600300080006009200000040001000", 96 ],
//		[ "000000039000001005003050800008090006070002000100400000009080050020000600400700000", 97 ],
//		[ "020403700000000032000000004040200070800050000000001000500000900030900007001008600", 98 ],
//		[ "001080604037600000500000000000005000006010800000400000000000003000007520802090700", 99 ],
//		[ "700000019460190000000682704090000007000300405006700000001000000200074000000200300", 94 ]
	["53.7...41...91....9..345...19..38....865...........8....4.6.....78......3...5..64", 30 ],
	["9...1.5.6...3.5.2....8..9.....5.3.........73...9.2..1......7...86.9..2.17...3..69", 30 ],
	["..84......15....9.2.47.5.....13......8..4.....2.1.93...92..7......93..8.7....4...", 30 ],
	["..........6.7....87543.1....46...97..1.9...5...8.4...1...6.........3961....57....", 30 ],
	["24.....8...3.7.....982.374.3.9...6.1..1.......2.......8...12......3674...1...8..9", 30 ],
	["5.......4.4.....68329....5........92...5..4...9.....3..572.3........8....8.9.6..7", 30 ],
	[".3.98..5.....35.....24.1.9.96........53...6.14........6..8..1.4.9....3...8......5", 30 ],
	[".4.7.8.....9..6.................9..11...7.8....51..4...9.48......8.67.5...2..59.7", 30 ],
	[".1....3.84.7....5.68.5.....3..7.....8...46............73.1..8.5.2...4.7...4..5..2", 30 ],
	["2...17..5.......2.76.9..3...1..6..4..4..32..9......6.........6......35.1...24..9.", 30 ],
	["5.....637.....4.181....6...7...4598...6...7........4.58.1......34..598.....3....4", 30 ],
	[".5.....9.....49.8..........1..9..8..49.35...28..4..6..5.3..61.896...2.4...2......", 30 ],
	["4.627..5.2....8.14....3.2.......4...7.3..56.8.25.6.1..........3..........81...4..", 30 ],
	["....48.1.2....3.98....6.2.......13..........9....2.64.....5....81...4.3.6.4.3215.", 30 ],
	[".5.6.1...972...1.......2.4....269.......7.......4.5....87.4..15..5.8..96.....74..", 30 ],
	["8.4.....9.........3...89.......7..58..3..2...4..95...2...1.75..9....43.7...2..8.4", 30 ],
	["....9...52..18..9.3....57..8....2....4.......7.5.......8..7...2..45..8.719.4...3.", 30 ],
	["21.68..9.4.61.3..29.......8..5...4.......2..3...9.465.....19.........285...2.....", 30 ],
	["..83...........2.6...9.6.....489...2.9....1.4.2...13.....1.74..17...3.8....6....7", 30 ],
	[".....4.........31...3.12.8..84.23.6..695...3.1...98..5.4.....28......941......6..", 30 ],
	[".........75..1......3.75.....432..9..12..94.......6.5..9..61...26..3.1.44......7.", 30 ],
	[".8...63....657..1...51..8..3........91...7.8.....8...5...21...6.........6.2.....3", 30 ],
	["...6.4....9...52.3..72....4.7........3....5..4.2....86...89..3...5..6.28.4.......", 30 ],
	["..83..465..5...8....7...........45.9.....2....5..9.67....8.9.4.6.3.7.......56..2.", 30 ],
	["...3.618...25..93...........5..1.....6.49.......6.8.5.84..32..5.9...4....1.9...6.", 30 ],
	[".6.1....7.................319.....843..6.82..2.....37...3..1.9.614.8..2...87.46..", 30 ],
	["72.8.49....9.3..67...........3....818713...92...........27...3.4........3..4..678", 30 ],
	[".57.8.61.4...1.2.....7...4...3.65....4......21.5........8......271......3....2..5", 30 ],
	["..8..9.......4...137.....6.8...1.3...5....6..94...3..74.7..6.....518.....6..54...", 30 ],
	["2..8.7.14...64.7.2.......3.36...89....91...73.......8....76.2.8...214..........9.", 30 ],
	["..9.476...3.5.......2169...4...7.2..1......3..7..9..64..39...41...6.2............", 30 ],
	["12..7..8.5.82....63..9...5..8......3...6............95..71.29.....7........563..2", 30 ],
	["...............27....9..6....1.73..5.5......2..4..291.73..4...9...5.9....9.63.1..", 30 ],
	[".92.....43.8...6216...8.3.....6..4........9.....5...3.8....2....2.3...6...4..9.15", 30 ],
	["...8..65165.7....4..8..47...4......6..168..97..29..8..4........2....19...........", 30 ],
	[".13..59.645.6..1....8.2....64.892..............5431....2..685............91......", 30 ],
	[".5.....8..417.....8.7...1....39.581...8...9..49.1.....9..8.4.7.....5.3..284....5.", 30 ],
	[".1....3..7........9.4.......2.......3...9.6..1.6...872.6.3.97....3.1798.4...82..6", 30 ],

	[".....9...2...3..1.7.3..4.2....71649..........5...9...69...6...2...42..513....7...", 55 ],
	["6......5....5461.98.1.7.3....4.9....7.......3....1......63...4...3..5.98..89.1...", 55 ],
	[".631...5.8.7.4...34....2....1...4.87.42...3......3....7..52....2..4.1..........3.", 55 ],
	[".3...1.....2347...4...2........1.7...4.962...9.6.......8...4...21..7.4.67.....32.", 55 ],
	[".6..12..........4.....5........9..5.9....127635.62.4.1...3.....4....97..5.....169", 55 ],
	["..51..3...31....94...9.27........957.8...........79.43...........2..4.89.57......", 55 ],
	["......4.......5.3....236....9.4.1..61.....3..4.2..985...896.2.5.1..8...........8.", 55 ],
	["5.4....7...6.9.38.....46.2.....3....9782..4...4.8......3.....5.259.........7.....", 55 ],
	[".1...423736.8...4.....7..1....713....3..8.......6......7....6.51.8.5.......24.1..", 55 ],
	["7.......269....5....8451..6...1........946...2478......89....4.....24.6..........", 55 ],
	["5....3..1..68.....2.3.6....7.1..89.........3..9....8.23...19..66..2...19........8", 55 ],
	["4.1.5.8..........3.....7.4.....74.59...1.97....4....6.5..9..4.6.....5......768.91", 55 ],
	[".186..4..3..1..5.7.4.7.3.8..5....7.8........3.9...........1..7.9..3.....4...851.9", 55 ],
	["4.7.....8.63.......8.......7.8.4..............5..86..75.61..8.9..4.57..3.31..8.4.", 55 ],
	["3.6....2...7....9..9.4..61.5.8.4...2...1.........27.34...69...8.....27......1....", 55 ],
	[".....2.9636..98..28......43..48.3...7...........1.......2.14...4....9.3.9.....72.", 55 ],
	["13.89.2...89.4...52461.....4...3.1.8....1.........6..9....6.8...1...84.........2.", 55 ],
	["...7.482.7.4.2.9...3..9145......91............1.8.....5...72..31....5..83.9......", 55 ],
	["....1...6...9.4......6..4.18....9.........51.64......7.5...6.92...52.17..72....4.", 55 ],
	["9........6...521..57.69......8..1.2...5...74..97.4.5.......9..1......456....6....", 55 ],
	["92......3.58.62......7.........169.2.1.2.....7..5..1..5.........86.79.3..79......", 55 ],
	["...7...2.........17.246..5.86....9...1..8...5..72........9....4..4.27819.........", 55 ],
	["1.28..6....396..8268.....1...............9..19....5....246..5..8.1.....45.9....63", 55 ],
	["....5..3..1........9..6....4....91...5..7.4....72....37....3..9.465...1...5..726.", 55 ],
	[".1.92.57.......9..........3.8.3...41........5...1.4.3.....9...7.917.3.82.45......", 55 ],
	["....21.7...6...1..4..79....7......63..347..5.6.45...9..9.............7..5...4..2.", 55 ],
	["..1.39.5......6....3.7...4......7...98.1..5.....8..1....34....7..6.......7...89.6", 55 ],
	[".....7.194.....6.3............1...7..6.49.5..31.6......3...........4932...7..2..5", 55 ],
	["..7..2........6.....1...6........19..2..1......497.5.217.6..4.5..3...2...6..537..", 55 ],
	["1....57.4.3.6.851....1.4.9.........3..9....4......1...4..35..6...18.943...7......", 55 ],
	["2...1.4.9.7.9.5..658..2....8.4.....7...1..9.471...8............9.8.....5.2.....1.", 55 ],
	["...25........9....2.9...3...5...1.2.63..4.8...4..896.....3.......58...9.....26..1", 55 ],
	[".....923.......6.55..1.3..7..6....4.......7....7..15..72.3.5..4..5.163...9..2....", 55 ],
	["......8723.4..95.......1......1.4..58..753...4.1...3.........2..87.9....5..4.....", 55 ],
	["...7........1..5.45.....2.737..956.2.1........4.32..9.2..6794.....24...6.........", 55 ],
	["4..........3...1...953......5..48.927....5..1.2...1.74....19.47249..3............", 55 ],
	[".98......32...65..67................96..27......85...6..6.4..3743.7.9.28....8....", 55 ],
	["2..1.....7...9.......46....6....249...7....6.4..31.......6..1.8..8....43.1..83..2", 55 ],
	["..69..1...2..3...4.714..9..79..5..6...52.7...............6...5.6.....2713........", 55 ],
	["7..5.....1......62.92...1.4.2.....5......5986..96....7.6.7....5.......9.2...98...", 55 ],

	["37.4.615.....9....46.....73...874......9.5.48.........7..68..94......8...5.......", 70 ],
	[".8..94..26.9.......2..75...4.....2.8........4.68....73....6..35.9..28.67....3....", 70 ],
	["8....574.75...2.....4.....5.7.9....66..4..3...42....9......7...9.....8.....6.8.2.", 70 ],
	[".5...7..........1.9461.........71.8...8.25..156.........5..8....9...2.7.67.....9.", 70 ],
	["1...6.........135..5.9.3.82379..56........59...............9.35....187..6.2......", 70 ],
	["5..1....93...982...6..2543.43..1.69.6..4.......8..63...........1....492.....5....", 70 ],
	[".7.23....2....1.......5....8....2....54..83......7.5.6....1..5..873.4.1.13.......", 70 ],
	["15.......6....7.4....859..6....3....3...4.75.4.5..6.9...9.....8....6.3...3....4..", 70 ],
	[".6.7...1.....8.9...1..6...2...4...9...7......54.2.7..1..5..8......1.26....4.....5", 70 ],
	[".14......95....46............63.........6574..2..1....23.17..9.6.12.4..3.4...62.7", 70 ],
	["2....864..39......87.9......8....46.........3.43....177..23..........13..924..7..", 70 ],
	["..9.1.7......5......28..6...176.5....64.9........4..2...15.8...97........261..8..", 70 ],
	["14.7.......9......78.2.....67..9.28..2.6..1.79.4.7....3.....8...6..3.7.1.........", 70 ],
	["1.6..8..482.....1.45.3........8......1.53..69.6...92.13.........82...94.......83.", 70 ],
	["7....294.4..96...32..8.761...41.....9.....32..7..9.1....3.8..6......1....82......", 70 ],
	["..8.....5..5..9.1.7......2..7.5.1...189.64....5479............84.16.5.7...294....", 70 ],
	["..58..1..9.6..3.5..83.4.7.6.39..6.1.8....4.....479.8.......8....6.3..........7...", 70 ],
	["...1....37...92.5.....5..4137....4..........9..97.8..251.3.9....92..7............", 70 ],
	["15.2.7..6672...8...........523..81.47....5.83.....2....1.74...2.........4.73.....", 70 ],
	[".6..7.58...8......97.........379...67..4.1..5.4.5..91.......1..834.......5..8.7..", 70 ],

	["..9..5.14..2...5...7..62............9..1..4...4.3.6..9.3.........6..4....5.21....", 80 ],
	["...8.1.....86....4....2..7..6...8.3...13....9.87.6...27.2....9.4..7...8.....39...", 80 ],
	["..2..1........3.24.....7.15.3..4..7....1..4.3.15........8.......9.25.8...573....2", 80 ],
	["...2...3.....57......3....1..6..9.7.71....5..2.5.....8.3..75.9.5....63...7....12.", 80 ],
	[".6....71...........74...698.92.8..41.46...2..8........32..498.6....7..3....3...24", 80 ],
	["...5.2......8..........6.8.13...7..66....92.8.........71.63......9..8....2..7.5.3", 80 ],
	[".4..5..6....1..5.835....1...3.............689.8..7.4.56...357.4...74.816.........", 80 ],
	["...3.487.....2..19...............1..4.197.2.8...8....3.75....8.8..1........24.5..", 80 ],
	["61.....3.8..7...52.7....6.8......8.7...5.1.9.9.86..2......5..2.32.1.....5.......3", 80 ],
	["5.47.....8..6.4.3.....9....94.86....3.8.5.......413...7..5...6..5.3...17......94.", 80 ],
	["2....1.....1........6.923..3....4.....5...7...7.615.9....9...3.....5.6..7...4312.", 80 ],
	["..82..1..5..3......27..4.5...6...27........63.4..2...1...64........8.72..7..1.8..", 80 ],
	[".25.....4.4.5.6.......8....162....4....125.3..5........9.3...26....6..1.....9.5.8", 80 ],
	[".......1...8...572..2..1..3...75...8...6....5.2.....4.3.4..8....7531.28.....7...6", 80 ],
	["..89........26..94.....1......18...3.9......6.41..6......59..42.....23.1..3...8..", 80 ],
	["..1.7..6.8..........5..63..........5.....92.72.....1863.8....5..6.785....7..93...", 80 ],
	[".2..5.431.51..8..7..3....5..8...2.4......3....35781....1..........6.5.2.6..8.....", 80 ],
	[".6....8.9...3.2..4.8.4...5....1.....9......62..2...4.85......9.....3.1....98..2..", 80 ],
	["8..4.....6.3..1....4.6.7..2.....5.8..1..6......4...6.75..27......7.5...4..1.....3", 80 ],
	[".1.........24.6..1.36...2.762....4.......1......3.......78..642..4.1..7.....5.3..", 80 ]

	],
init: function() {
	var file = Components.classes["@mozilla.org/file/directory_service;1"]
                     .getService(Components.interfaces.nsIProperties)
                     .get("ProfD", Components.interfaces.nsIFile);
	file.append("grids.sqlite");

	var storageService = Components.classes["@mozilla.org/storage/service;1"]
                        .getService(Components.interfaces.mozIStorageService);
	var createdb = 0;
	try {
		this.conn = storageService.openDatabase(file);
		// check whether we have got the tables
		var statement = this.conn.createStatement("SELECT count(*) FROM sqlite_master WHERE type='table' and name='grids' ");
		while (statement.executeStep()) {
			var value = statement.getInt32(0);
			if (!value) {
				// initialize the database
				createdb = 1;
			}
		}
	} catch(e) {
		LOG("Exception: " + e);
		createdb = 1;
		file.remove(false);
		this.conn = storageService.openDatabase(file);
	}
	if (createdb) {
		this.createdb();
	}
},
has_grid: function(grid) {
	var statement = this.conn.createStatement("SELECT id FROM grids WHERE grid=?1");
	statement.bindStringParameter(0, grid);
	if (statement.executeStep()) {
		return statement.getInt32(0);
	}
	return null;
},
addgrid: function(grid, rating, gid) {
	var id = griddb.has_grid(grid);
	if (null!=id) {
		LOG("Already have this grid: " + id);
		return id;
	}
	var statement = this.conn.createStatement("INSERT INTO grids (id, gid, grid, rating) values (NULL, ?1, ?2, ?3)");
	if (null==gid) {
		statement.bindNullParameter(0);
	} else {
		statement.bindInt32Parameter(0, gid);
	}
	statement.bindStringParameter(1, grid);
	statement.bindInt32Parameter(2, rating);
	statement.execute();
	var id = this.conn.lastInsertRowID;
	LOG("INSERTED: " + id);
	return id;
},
createdb: function() {
	LOG("Creating griddb");
	/**
	 * id		the local grid id.
	 * gid		the global (server) grid id. null if imported grid.
	 * grid		the grid definition
	 * rating	1-99, 99 is the most difficult
	 * date		the date of the last time it was finished, 0 meaning never
	 *	 		day = date & 0xFF, 
	 *			month = date>>8 & ff, 
	 *			year = date>>16 & 0xFFFF
	 * best		the best time in s
	 * sum		the sum of times in s
	 * count	how many times it has been tried
	 */
	this.conn.executeSimpleSQL("CREATE TABLE grids (id INTEGER PRIMARY KEY, gid INTEGER, grid TEXT UNIQUE, rating INTEGER, date INTEGER, best INTEGER, sum INTEGER, count INTEGER)");
	var i = 1;
	var s1 = this.conn.createStatement("INSERT INTO grids (id, gid, grid, rating, date, best, sum, count) values (?1, NULL, ?2, ?3, 0, 0, 0, 0)");
	for (var i = 0;i<griddb.init_data.length;i++) {
		s1.bindInt32Parameter(0, i+1);
		s1.bindStringParameter(1, griddb.init_data[i][0]);
		s1.bindInt32Parameter(2, griddb.init_data[i][1]);
		s1.execute();
	}
	LOG("Creating griddb - done");
},
get_unfinished_grid: function(difficulty) {
	var s1;
	if (played) {
		s1 = this.conn.createStatement("SELECT id, gid, grid, rating, date, best, sum, count FROM grids WHERE rating between ?1 AND ?2 AND count>0 LIMIT 1");
	} else {
		s1 = this.conn.createStatement("SELECT id, gid, grid, rating, date, best, sum, count FROM grids WHERE rating between ?1 AND ?2 AND 0=count LIMIT 1");
	}
	if (difficulty<=1) {
		s1.bindInt32Parameter(0, 1);
		s1.bindInt32Parameter(1, 50);
	} else if (2==difficulty) {
		s1.bindInt32Parameter(0, 51);
		s1.bindInt32Parameter(1, 60);
	} else {
		s1.bindInt32Parameter(0, 61);
		s1.bindInt32Parameter(1, 1000);
	}
	var ret = new Object();
	if (s1.executeStep()) {
		return new GRID(
			s1.getInt32(0),
			s1.getInt32(1),
			s1.getString(2),
			s1.getInt32(3),
			s1.getInt32(4),
			s1.getInt32(5),
			s1.getInt32(6),
			s1.getInt32(7));
	}
	return new GRID(
		null, null,
		"502400000809003040010950302000039060700000003090610000903046070020800906000005408",
		50, 0, 0, 0, 0);
},
get_grids: function() {
	var ret = [];
	var s1 = this.conn.createStatement("SELECT id, gid, grid, rating, date, best, sum, count FROM grids ORDER BY date");
	while (s1.executeStep()) {
		ret.push(new GRID(
			s1.getInt32(0),
			s1.getInt32(1),
			s1.getString(2),
			s1.getInt32(3),
			s1.getInt32(4),
			s1.getInt32(5),
			s1.getInt32(6),
			s1.getInt32(7)
		));
	}
	return ret;
},
get_grid_by_id: function(i) {
	var ret = [];
	var s1 = this.conn.createStatement("SELECT id, gid, grid, rating, date, best, sum, count FROM grids WHERE id=?1");
	s1.bindInt32Parameter(0, i)
	while (s1.executeStep()) {
		return new GRID(
			s1.getInt32(0),
			s1.getInt32(1),
			s1.getString(2),
			s1.getInt32(3),
			s1.getInt32(4),
			s1.getInt32(5),
			s1.getInt32(6),
			s1.getInt32(7)
		);
	}
	return null;
},
get_grid_by_pos: function(i) {
	var s1 = this.conn.createStatement("SELECT id, gid, grid, rating, date, best, sum, count FROM grids ORDER BY id LIMIT 1 OFFSET ?1");
	s1.bindInt32Parameter(0, i)
	while (s1.executeStep()) {
		return new GRID(
			s1.getInt32(0),
			s1.getInt32(1),
			s1.getString(2),
			s1.getInt32(3),
			s1.getInt32(4),
			s1.getInt32(5),
			s1.getInt32(6),
			s1.getInt32(7)
		);
	}
	return null;
},
get_random_grid: function(i) {
	var s1 = this.conn.createStatement("SELECT COUNT(*) FROM grids");
	if (s1.executeStep()) {
		var count = s1.getInt32(0);
		var r = Math.floor(Math.random()*count);
		return this.get_grid_by_pos(r);
	}
	return new GRID(
		null, null,
		"502400000809003040010950302000039060700000003090610000903046070020800906000005408",
		50, 0, 0, 0, 0);
},
/**
 * g is the grid
 * gid is the gid if known, null if this grid is not free, and -1 if the grid can be submitted to the server.
 */
introduce: function(g, gid) {
	g = g.replace(/[^0-9]/g, '0');
	var id = griddb.has_grid(g);
	if (null==id) {
		id = griddb.addgrid(g, -1, gid);
	}
	var s1 = this.conn.createStatement("SELECT id, gid, grid, rating, date, best, sum, count FROM grids WHERE id=?1");
	s1.bindInt32Parameter(0, id);
	if (s1.executeStep()) {
		return new GRID(
			s1.getInt32(0),
			s1.getInt32(1),
			s1.getString(2),
			s1.getInt32(3),
			s1.getInt32(4),
			s1.getInt32(5),
			s1.getInt32(6),
			s1.getInt32(7));
	}
	return new GRID(null, null, gid, 50, 0, 0, 0, 0);
},
add_score: function(grid, secs) {
	grid.sum+= secs;
	grid.count++;
	if (0==grid.best || grid.best>secs) {
		grid.best = secs;
	}
	grid.set_date();
	var s1 = this.conn.createStatement("UPDATE grids SET date=?1, best=?2, sum=?3, count=?4 WHERE id=?5");
	s1.bindInt32Parameter(0, grid.dt);
	s1.bindInt32Parameter(1, grid.best);
	s1.bindInt32Parameter(2, grid.sum);
	s1.bindInt32Parameter(3, grid.count);
	s1.bindInt32Parameter(4, grid.id);
	s1.execute();
},
__sentinel: 0 //so that we can write a , after every bloody member.
};

griddb.init();

